<template>
  <div class="app-container customer-div">
    <el-menu :default-active="listQuery.tab" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item index="my" v-if="procurementInventoryForMy">我的</el-menu-item>
      <el-menu-item index="all" v-if="procurementInventoryForAll">全部</el-menu-item>
    </el-menu>
    <div style="margin-top: 40px">
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
        <el-form-item label="采购订单号：">
          <el-input v-model="listQuery.orderSn" clearable></el-input>
        </el-form-item>
        <el-form-item label="订单日期：">
          <el-date-picker
                  v-model="listQuery.createTime"
                  type="daterange"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="采购员：">
          <el-select v-model="listQuery.userId" filterable placeholder="请选择" clearable>
            <el-option
                    v-for="item in userOptionList"
                    :key="item.userId"
                    :label="item.userName"
                    :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="供应商：">
          <el-select
            clearable
            v-model="listQuery.supplierId"
            filterable
            reserve-keyword
            placeholder="请输入关键词"
            style="width: 220px;">
            <el-option
              v-for="item in options2"
              :key="item.supplierId"
              :label="item.supplierName"
              :value="item.supplierId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单是否关闭：">
          <el-select v-model="listQuery.isClosed "  placeholder="请选择" clearable>
            <el-option label="是" :value="1"></el-option>
            <el-option label="否" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="handleSearchList()"
            type="primary">
            查询
          </el-button>
          <el-button
            :loading="btnLoading"
            @click="downloadExcel()"
            type="primary">
            导出
          </el-button>
        </el-form-item>
      </el-form>
    </div>
      <!--数据列表-->
      <div class="table-container">
        <el-table
          v-loading="listLoading"
          :data="tableData"
          border
          style="width: 100%; margin-top: 20px">
          <el-table-column  label="供应商" width="200" align="center" prop="supplierName">
            <template slot-scope="scope">
              {{scope.row.supplierName}}
            </template>
          </el-table-column>
          <el-table-column  label="采购订单号" width="140" align="center" prop="orderSn">
            <template slot-scope="scope">
<!--              <el-link @click="toDetail(scope.row)" type="primary">-->
<!--                {{scope.row.orderSn}}-->
<!--              </el-link>-->
              {{scope.row.orderSn}}
            </template>
          </el-table-column>
          <el-table-column  label="订单是否关闭" width="140" align="center" prop="orderSn">
            <template slot-scope="scope">
              {{scope.row.isClosed == 1 ? "是" : "否"}}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="采购员"
            width="100">
            <template slot-scope="scope">
                {{scope.row.userName}}
            </template>
          </el-table-column>
          <el-table-column
                  label="采购数量"
                  width="150"
                  align="center">
            <template slot-scope="scope">
              {{scope.row.orderGoodsNum}}
            </template>
          </el-table-column>
          <el-table-column
            prop="orderAmount"
            align="center"
            label="合同总价"
            width="150">
          </el-table-column>
          <el-table-column
            align="center"
            width="150"
            label="平均单价">
            <template slot-scope="scope">
                {{scope.row.averagePrice}}
            </template>
          </el-table-column>
          <el-table-column
                  align="center"
                  width="150"
                  label="已付款金额">
              <template slot-scope="scope">
                {{scope.row.paidAmount}}
              </template>
          </el-table-column>
          <el-table-column
            label="已入库数量"
            width="150"
            align="center">
            <template slot-scope="scope">
              {{scope.row.orderGoodsInNum}}
            </template>
          </el-table-column>
          <el-table-column
                  label="已入库已付款数量"
                  width="150"
                  align="center">
            <template slot-scope="scope">
              {{scope.row.hasInPaidNum}}
            </template>
          </el-table-column>
          <el-table-column
                  label="已入库未付款数量"
                  width="150"
                  align="center">
            <template slot-scope="scope">
              {{scope.row.hasInUnPaidNum}}
            </template>
          </el-table-column>
          <el-table-column
            label="未入库数量"
            width="150"
            align="center">
            <template slot-scope="scope">
              {{scope.row.orderGoodsUnInNum}}
            </template>
          </el-table-column>
          <el-table-column
            label="未入库已付款数量"
            width="150"
            align="center">
            <template slot-scope="scope">
              {{scope.row.unInPaidNum}}
            </template>
          </el-table-column>
          <el-table-column
                  label="未入库未付款数量"
                  width="150"
                  align="center">
            <template slot-scope="scope">
              {{scope.row.unInUnPaidNum}}
            </template>
          </el-table-column>
          <el-table-column
                  label="未付款金额"
                  width="150"
                  align="center">
            <template slot-scope="scope">
              {{scope.row.unPaidAmount}}
            </template>
          </el-table-column>
        </el-table>
      </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="count">
      </el-pagination>
    </div>

  </div>
</template>
<script>
  import {getOrderUsersList,consultationList,supplierList} from '@/api/partner'
  import {purchaseOrderAnalysis} from '@/api/company'
    import {formatDate} from '@/utils/date';
    import store from '@/store'
    import { mapGetters } from 'vuex'
  import {capitalCostList} from "../../api/partner";
    const defaultListQuery={
      "tab" : "",
      "createTime" : [],
      "userId" : '',
      supplierId:'',
      isClosed:'',
      "pageNum" : 1,
      "pageSize" : 10
    };

    export default {
      name: "procurementInventory",
      data(){

          return {
              listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
              tableData: [],
              count: 0,
              listLoading: false,
              userOptionList:[],
              roles:store.getters.roles ? store.getters.roles : [],
            options2:[],
            btnLoading:false
          }
      },
      computed: {
        ...mapGetters([
          'name',
          'mobile',
          'userId'
        ]),
        procurementInventoryForMy(){
          if(this.roles.indexOf("procurementInventoryForMy") != -1){
            return true
          }else{
            return false
          }
        },
        procurementInventoryForAll(){
          if(this.roles.indexOf("procurementInventoryForAll") != -1){
            return true
          }else{
            return false
          }
        },
      },
      created() {
        if(this.procurementInventoryForMy){
          this.listQuery.tab = 'my'
        }else if(this.procurementInventoryForAll){
          this.listQuery.tab = 'all'
        }
        this.getList();
        this.getUserOptionList();
        this.getSupplierList()
      },
      filters: {

      },
      methods:{
        //下载excle
        downloadExcel(){
          let params = JSON.parse(JSON.stringify(this.listQuery));
          params.isExport = 1;
          delete params.keywords;
          delete params.pageNum;
          delete params.pageSize;
          this.btnLoading = true;
          purchaseOrderAnalysis(params).then((res)=>{
            this.btnLoading = false;
            require.ensure([], () => {
              const { export_json_to_excel } = require('sheet/Export2Excel');
              const tHeader = res.title;
              const filterVal = res.columns;
              const list = res.list;
              const data = this.formatJson(filterVal, list);
              export_json_to_excel(tHeader, data, res.fileName);
            })
          }).catch(()=>{
            this.btnLoading = false;
          })
        },

        //下载表格数据格式化
        formatJson(filterVal, jsonData) {
          return jsonData.map(v => filterVal.map(j => v[j]))
        },

        //供应商
        getSupplierList() {
          supplierList().then((res)=>{
            this.options2 = res;
          })
        },
        //去详情页
        toDetail(item){
          let routeData = this.$router.resolve({name:'saleDetail',query:{id:item.orderId,}});
          window.open(routeData.href);
        },
        /**
         * 切换选择状态
         */
        handleSelect(key, keyPath) {
          this.listQuery = JSON.parse(JSON.stringify(defaultListQuery))
          this.listQuery.tab = key;
          this.handleSearchList();
        },
          //获取员工下拉选项
          getUserOptionList(){
            getOrderUsersList({departmentId:3,status:1}).then((res)=>{
                  this.userOptionList = res;
              })
          },


        handleSizeChange(val){
          this.listQuery.pageNum = 1;
          this.listQuery.pageSize = val;
          this.getList();
        },
        handleCurrentChange(val){
          this.listQuery.pageNum = val;
          this.getList();
        },

        handleSearchList(){
          this.listQuery.pageNum = 1;
          this.getList();
        },

        //获取列表
        getList() {
          this.listLoading = true;
          purchaseOrderAnalysis(this.listQuery).then(response => {
            this.tableData = response.list;
            this.count = response.count;
            this.listLoading = false;
          }).catch(()=>{
            this.listLoading = false;
          });
        },

      },
    }
</script>

<style lang="scss">

</style>
